import http from "../../../class/Utils/Http"
import tips from "../../../class/Utils/Tips"
import user from "../../../class/Utils/User"
Page({
    data: {
        bangdingtoocletag: false,
        testlist: [
            {id: 1},
            {id: 1},
            {id: 1},
            {id: 1},
            {id: 1},
        ],
        aa: 0,
        sctig: 0,//靠谱状态 1 靠谱+1
        kpshu: 121, //靠谱状态数,
        piclist: [],
        bofang: 0,
        bb: 0,
        yuyinqianmingtig: 0,
        openinfo: false,
      xiaoxi_count:0,
    },
    onLoad: function (get) {
        var that = this
        that.setData({
            id: get.id||0,
            login:get.login||0
        })
        if (get.q) {
          var link = decodeURIComponent(get.q);
          var paramArr = link.split('_');
          that.setData({
            id: paramArr[1],
          })
        }
        var user = wx.getStorageSync('user');
        console.log(user)
        if (!user) {
            that.setData({
                shouquan: 1
            })
        } else {
            that.setData({
                shouquan: 0
            })
            that.info()
        }


    },
    onShow:function () {
        var that = this
        var user = wx.getStorageSync('user');
        /*
         获取未读消息数量
         */
        http.req('/Mingpian/xiaoxi', { uid: user.userid }, function (res) {
            console.log(res)
            that.setData({
                xiaoxi_count: res
            })
        })
      wx.onBackgroundAudioStop(function () {
        console.log('onBackgroundAudioStop')
        that.setData({
          bofang: '0',
          yuyinqianmingtig: 0,
        })
      })
    },
    info: function () {
        var that = this
        var user = wx.getStorageSync('user');
        that.setData({
            user: user
        })
        wx.showLoading({
            title: '加载中...',
        })
        var post_data = {
            id: that.data.id,
            uid: user.userid,
            login:that.data.login
        }
        http.req("/Mingpian/chakan", post_data, function (infores) {
            console.log(infores)
            that.setData({
                sender:infores.sender,
                scuid: infores.id,
                sctig: infores.sc,
                sc_num: infores.dianzan
            })
            var content_pic = infores.content_pic;
            var piclist = that.data.piclist
            if (content_pic.length > 0) {
                for (var i = 0; i < content_pic.length; i++) {
                    piclist.push(content_pic[i].pic)
                    that.setData({
                        piclist: piclist
                    })
                }
            }
            that.setData({
                info: infores,
            })
            console.log("aaaa" + that.data.info.youxiang)
            that.wodekehulist(infores.id)

            setTimeout(function () {
                wx.hideLoading()
            }, 1000)
            if(user.log==1){
                http.req("/Mingpian/tianjia_haoyou", {haoyouid: infores.userid, uid: user.userid},function (resss) {
                    console.log(resss)
                })
            }
        })
        that.stopmuqianming()

    },
    /*
     跳转
     */
    goto: function (e) {
        var that = this
        /*
         log_user 返回  缓存user
         */
        user.log_user(this, function (res) {
            console.log(123)
            var url = e.currentTarget.dataset.url
            tips.goto(url)
        })
    },
    /*
    添加好友
     */
    tianjiahaoyou:function () {
        var that = this
        user.log_user(this, function (res) {
            var infores = that.data.info
            var user = wx.getStorageSync('user');
            var post_data = {
                haoyouid:infores.userid||0,
                uid: user.userid,
                haoyou_login:infores.wx_user.api.res.poster||0
            }
            console.log(post_data)
            http.req("/Mingpian/tianjia_haoyou", post_data,function (resss) {
                console.log(resss)
                tips.ok(resss.msg_val);
            })
        })

    },
    //获取我的客户数据
    wodekehulist: function (id) {
        var that = this
        var user = wx.getStorageSync('user');



        //获取我的客户数据
      var data = {
        mid: id,
      }
      console.log(data)
        http.req("/Mingpian/liulan_renyuan", data, function (res) {
            console.log(res)
            that.setData({
                renqi:res.renqi,
                mykehulist:res.list
            })

        })




    },
    //下拉刷新
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading()

    
      var that = this
      var user = wx.getStorageSync('user');
      that.setData({
        user: user
      })
      wx.showLoading({
        title: '更新信息...',
      })
        var post_data = {
            id: that.data.id,
            uid: user.userid,
            login:that.data.login
        }
      http.req("/Mingpian/chakan", post_data, function (infores) {
        console.log(infores)
        that.setData({
          sender: infores.sender,
          scuid: infores.id,
          sctig: infores.sc,
          sc_num: infores.dianzan
        })
        var content_pic = infores.content_pic;
        var piclist = that.data.piclist
        if (content_pic.length > 0) {
          for (var i = 0; i < content_pic.length; i++) {
            piclist.push(content_pic[i].pic)
            that.setData({
              piclist: piclist
            })
          }
        }
        that.setData({
          info: infores,
        })
        that.wodekehulist(infores.id)

        setTimeout(function () {
          wx.hideLoading()
        }, 1000)
        if (user.log == 1) {
          http.req("/Mingpian/tianjia_haoyou", { haoyouid: infores.userid, uid: user.userid }, function (resss) {
            console.log(resss)
          })
        }
      })


      
      that.stopmuqianming()
      
      wx.stopPullDownRefresh()
      wx.hideNavigationBarLoading()
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
    
    },
    previewImage: function (e) {
        var that = this
        var current = e.currentTarget.dataset.src;
        wx.previewImage({
            current: current,// 当前显示图片的http链接
            urls: that.data.piclist // 需要预览的图片http链接列表
        })
    },
    //添加到通讯录
    adddianhuabu: function () {
        var that = this
        wx.addPhoneContact({
            //lastName: that.data.lastName,
            photoFilePath: that.data.info.pic,//头像
            firstName: that.data.info.xingming,//联系人姓名
            organization: that.data.info.gongsi,//公司名称
            title: that.data.info.zhiwei,//公司职位
            mobilePhoneNumber: that.data.info.shouji,//联系人手机号
            weChatNumber: that.data.info.weixin,//微信号
            email: that.data.info.youxiang,//电子邮箱
            workAddressCity: that.data.info.dizhi,//工作城市
            workPhoneNumber: that.data.info.guhua,//办公电话
        })
    },
    //播放语音个人签名
    playyuyinqianming: function () {
        var that = this
        var yyqmtig = that.data.bofang
        console.log(yyqmtig)
        if (yyqmtig == 0) {
            wx.playBackgroundAudio({
                dataUrl: that.data.info.qianmingmp3,
                title: '个性签名',
            })
            that.setData({
                bofang: 1,
                yuyinqianmingtig: 1,
            })

        }
        if (yyqmtig == 1) {
            wx.stopBackgroundAudio()
            that.setData({
                bofang: '0',
                yuyinqianmingtig: 0,
            })
            console.log(yyqmtig)
        }


    },
    //跳转页面 停止播放
    onHide: function () {
        var that = this
        wx.stopBackgroundAudio()
        that.setData({
            bofang: '0',
        })
    },
    //自动停止播放语音签名
    stopmuqianming: function () {
        var that = this
        wx.onBackgroundAudioStop(function () {
            that.setData({
                yuyinqianmingtig: 0
            })
        })
    },
    //播放语音签名
    playyuyinqianmingb: function () {
        var that = this
        wx.playBackgroundAudio({
            dataUrl: that.data.yuyinqianming,
            title: '个性签名',

            success: function () {
                wx.getBackgroundAudioPlayerState({
                    success: function (res) {
                        var status = res.status
                        if (status == 1) {
                            wx.stopBackgroundAudio()
                            that.setData({
                                bofang: '0',
                            })
                        }
                    }
                })
            }

        })
    },
    //点击加减靠谱
    aabb: function () {
        var that = this
        var user = wx.getStorageSync('user');
        var sc_num = that.data.sc_num
        var stig = that.data.sctig//1是靠谱 0是没选
        var kptig = that.data.kpshu//靠谱当前的数
        var aatig = that.data.aa// 1是运行红心动画 0是隐藏红心动画
        if (stig == 0) {
            that.setData({
                aa: 1,
                sctig: 1,
                jia1: '+1',
                kpshu: kptig + 1,
                sc_num: parseInt(sc_num) + 1

            })
            setTimeout(function () {
                that.setData({
                    aa: 0,
                })
            }, 1000)
            http.req("/Mingpian/mp_sc", {userid: user.userid, id: that.data.info.id})
        } else {
            that.setData({
                aa: 1,
                sctig: 0,
                jia1: '-1',
                kpshu: kptig - 1,
                sc_num: parseInt(sc_num) - 1
            })
            setTimeout(function () {
                that.setData({
                    aa: 0,
                })
            }, 1000)
            http.req("/Mingpian/del_mp_sc", {userid: user.userid, id: that.data.info.id})
        }

    },
    onShareAppMessage: function () {
        var that = this
        return {
            title: '我是' + that.data.info.gongsi + "的" + that.data.info.xingming,
            path: '/pages/mingpian/card/card?id=' + that.data.info.id,
            imageUrl: that.data.info.pic,

            success: function (res) {
                http.req("/Mingpian/fenxiang", {id: that.data.info.id}, function (res) {

                    tips.ok("转发成功")

                })
            },
        }

    },
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this, function () {
            that.setData({
                shouquan: 0
            })
            wx.showLoading({
                title: '加载中...',
            })
            that.info()
        })


    },
    /*
     弹出 登录
     */
    bangdingtoocle: function () {
        var that = this
        that.setData({
            bangdingtoocletag: !that.data.bangdingtoocletag
        })
    },
    /*
     点击登录
     */
    denglu: function (e) {
        var that = this
        var zhanghao = e.detail.value.zhanghao
        var mima = e.detail.value.mima
        if (!zhanghao) {
            tips.error("账号不能为空");
            return false
        }
        if (!mima) {
            tips.error("密码不能为空");
        }
        http.req("/Api/log", {login: zhanghao, passwd: mima, uid: wx.getStorageSync('user').userid}, function (res) {
            console.log(res)
            if (res.code == '200') {
                var user = wx.getStorageSync('user')
                user.log = 1
                wx.setStorageSync('user', user)
                tips.ok('登录成功')
                that.setData({
                    userinfo: user,
                    bangdingtoocletag: false,
                    shengyihao: zhanghao
                })
            } else {
                tips.error(res.msg_val)
            }
        })
    },
    //去我的
    gotomain: function () {
        wx.switchTab({
            url: '/pages/main/menu/menu'
        })
    },
//去首页
    gotohome: function () {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },
    //去名片
    gotowodemingpian: function (e) {
        wx.redirectTo({
            url: '/pages/mingpian/card/card?id=' + this.data.info.id
        })
    },
    // //去旺铺
    // gotowangpu:function(){
    //     wx.redirectTo({
    //         url: '/pages/mingpian/wangpu/index/index?mid='+this.data.id
    //     })
    // },
    // //去企业介绍
    // gotojieshao:function(){
    //     wx.redirectTo({
    //         url: '/pages/mingpian/wangpu/jieshao/jieshao?mid=' + this.data.id
    //     })
    // },
    //去旺铺
    gotowangpu: function () {
      var that = this
        var info = this.data.info
        if (info.wx_user.toocle_cid == 0) {
            tips.error("暂无旺铺")
            return false
        }

      tips.redirectTo("/pages/toocle/shop/index/index?cid=" + info.wx_user.toocle_cid + "&time=" + info.wx_user.toocle_ctime + "&quanmenuid=1&userid=" + info.wx_user.id + "&menus=2" + "&mid=" + that.data.info.id);


    },
    //去企业介绍
    gotojieshao: function () {
        var info = this.data.info
        if (info.wx_user.toocle_cid == 0) {
            tips.error("暂无旺铺")
            return false
        }

            tips.redirectTo("/pages/toocle/shop/index/index?cid=" + info.wx_user.toocle_cid + "&time=" + info.wx_user.toocle_ctime + "&quanmenuid=3&userid="+info.wx_user.id);

    },
    //改变名片展开信息
    changempinfo: function () {
        var that = this
        that.setData({
            openinfo: !that.data.openinfo
        })
    },
  //去生意宝服务中心
  // gotofuwuzhongxin: function () {
  //   tips.redirectTo('/pages/mingpian/tooclefuwu/tooclefuwu');
  // },
  //去生意宝服务中心
  gotofuwuzhongxin: function () {
    tips.godaohang('/pages/main/menu/menu');
  },
  //拨打手机
  callshouji: function () {
    var that = this
    wx.makePhoneCall({
      phoneNumber: that.data.info.shouji,
      success: function () {
        console.log("成功拨打电话")
      }
    })
  },
  //拨打固话
  callguhua: function () {
    var that = this
    wx.makePhoneCall({
      phoneNumber: that.data.info.guhua,
      success: function () {
        console.log("成功拨打电话")
      }
    })
  },
  //获取绑定微信的手机号码
  getPhoneNumber: function (e) {
    var that = this
    user.getPhoneNumber(e, that)
  },


})